//
//  BoZeCarListVC.swift
//  BoZeCalendar
//
//  Created by 李东 on 2024/4/11.
//

import UIKit
import SnapKit

class BoZeCarListVC: BoZeBaseVC {
    
    lazy var emptyLabel = UILabel()
    lazy var tableView = UITableView(frame: .zero, style: .grouped)
    let CellID = NSStringFromClass(BoZeCarCell.self)
    lazy var dataList = [BoZeCarModel]()
    lazy var addBtn = UIButton(type: .custom)

    override func viewDidLoad() {
        super.viewDidLoad()

        hideNavgaitionBar()
        showBgImgView = true
        bgImgView.image = UIImage(named: "img_me_bg")
        showPopBtn = true
        navTitle = "加油啦-油耗管家"
        configUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        dataList = BoZeDataHelper.shared.getLocalList()
        tableView.reloadData()
        emptyLabel.isHidden = !dataList.isEmpty
    }
    
    func configUI() {
        addBtn.setImage(UIImage(named: "img_add_oil"), for: .normal)
        addBtn.addTarget(self, action: #selector(addCar), for: .touchUpInside)
        view.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.width.height.equalTo(44)
            make.centerY.equalTo(popBtn)
            make.right.equalTo(-10)
        }
        
        emptyLabel.font = UIFont.systemFont(ofSize: 16)
        emptyLabel.textColor = .white
        emptyLabel.numberOfLines = 0
        emptyLabel.textAlignment = .center
        emptyLabel.text = "请点击右上角 [+] 按钮\n添加你的车辆"
        view.addSubview(emptyLabel)
        emptyLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-50)
        }
        
        tableView.separatorStyle = .none
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.backgroundColor = .clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 56
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 20))
        tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 10))
        tableView.register(UINib(nibName: "BoZeCarCell", bundle: nil), forCellReuseIdentifier: CellID)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(CGFloat.navHeight)
        }
    }
    
    @objc func addCar() {
        navigationController?.pushViewController(BoZeAddCarVC(), animated: true)
    }


}

extension BoZeCarListVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        dataList.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: CellID, for: indexPath) as! BoZeCarCell
        cell.model = dataList[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        navigationController?.pushViewController(BoZeCarOilDetailVC(model: dataList[indexPath.section]), animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        10
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        UIView()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        UIView()
    }
}


